package JdbcTest.Jdbc;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/*
数据库删除操作
 */
public class JdbcTest2 {
    public static void main(String[] args){
        Connection conn = null;
        Statement sta = null;
        FileInputStream fis = null;
        Properties map = new Properties();
        try {
            //利用io流来读取文件中数据库连接信息
            fis = new FileInputStream("src/JdbcTest/Jdbc/DatabaseFile.properties");
            map.load(fis);
            String driver = map.getProperty("Driver");
            String url = map.getProperty("url");
            String username = map.getProperty("username");
            String password = map.getProperty("password");
            //1.注册驱动
            Class c = Class.forName(driver);
            //2.获取数据库连接对象
            conn = DriverManager.getConnection(url,username,password);
            //3.获取数据库操作对象
            sta = conn.createStatement();
            String sql = "delete from t_student where id = 5";
            //4.执行Sql语句
            int num = sta.executeUpdate(sql);
            if(num > 0){
                System.out.println("修改成功，共修改了"+num+"条数据");
            }else{
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally{
            if (sta != null) {
                try {
                    sta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
